<?php
//Coded by ahoangphuc
class AuthController extends Zend_Controller_Action
{	public $view;
	protected $title;
	
	
	public function init()
	{	$this->_helper->viewRenderer->setNoRender(); 
		$this->view = new Smarty_View();
		$this->view->assign("base_url", $this->_request->getbaseurl());
		Zend_Loader::loadClass('Test');
		echo 'aloha';
	}
	public function indexAction()
	{
		$this->_redirect('/');
	}
	public function loginAction()
	{
		$message = '';
		if($this->_request->isPost())
		{
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			$username = $filter->filter($this->_request->getPost('username'));
			$password = $filter->filter($this->_request->getPost('password'));
			$test = new Test();
			$info_admin=$test->admin_status($username);
			$this->view->assign("ad",$info_admin);
			
			if(empty($username))
			{
				$message.='Ten dang nhap ko hop le';
			}if(empty($password)) {
				$message.='<br>Mat khau khong hop le';
			} 
				if(empty($message))	
					{
					//Thiet lap Zend_Auth de thao tac voi database-table
					Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
					$db = Zend_Registry::get('db');
	                $authAdapter = new Zend_Auth_Adapter_DbTable($db);
					$authAdapter->setTableName('admin');
					$authAdapter->setIdentityColumn('admin_fullname');
					$authAdapter->setCredentialColumn('admin_status');
					$authAdapter->setCredentialColumn('admin_password');
					
					//Thiet lap gia tri Input vao trong bien
					$authAdapter -> setIdentity($username);
					$authAdapter -> setCredential('1');
					$authAdapter -> setCredential($password);
					
					//Xac thuc thong tin nhap vao
					$auth = Zend_Auth::getInstance();
					$result=$auth->authenticate($authAdapter);
					if($result->isValid())
					{
						$data = $authAdapter->getResultRowObject(array('admin_fullname', 'admin_type'));
	                    $auth->getStorage()->write($data);
	                    $this->_redirect('/');			
					} 
				}else {
					// failure: clear database row from session
                    $message .= '<br>Dang nhap khong thanh cong.';
                    }
			
		}
	
	$this->title="Login";
	$this->view->assign("title",$this->title);
	$this->view->assign("message", $message);
	$this->view->render('login.html');
	}
	 public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/index');
    }
	
}

?>